package com.stormrage.edu.serv.edu.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.edu.EduMajorDao;
import com.stormrage.edu.dao.edu.EduModuleDao;
import com.stormrage.edu.dao.edu.view.EduMajorModuleDao;
import com.stormrage.edu.dao.edu.view.EduProjectMajorDao;
import com.stormrage.edu.serv.edu.EduMajorServ;
import com.stormrage.edu.serv.edu.view.EduMajorModuleServ;
import com.stormrage.entity.edu.EduMajor;
import com.stormrage.entity.edu.EduModule;
import com.stormrage.entity.edu.rep.EduMajorRep;
import com.stormrage.entity.edu.req.EduMajorReq;
import com.stormrage.entity.edu.view.EduMajorModule;
import com.stormrage.entity.rep.ListRep;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Service
public class EduMajorServImpl implements EduMajorServ {

	private static final Logger logger = LoggerFactory.getLogger(EduMajorServImpl.class);

	@Autowired
	private EduMajorDao eduMajorDao;

	@Autowired
	private EduModuleDao eduModuleDao;

	@Autowired
	private EduMajorModuleDao eduMajorModuleDao;

	@Autowired
	private EduProjectMajorDao eduProjectMajorDao;

	@Autowired
	private EduMajorModuleServ eduMajorModuleServ;

	@Override
	public Integer create(final EduMajor arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 Create 】- 开始运行 ");

		Integer index = eduMajorDao.create(arg0);

		if (arg0.getModule().equals(0)) {

			EduModule mod = EduModule.instance(arg0.getMajor() + " - 必修课组", "必修课组", 0);
			Integer id = eduModuleDao.create(mod);
			EduMajorModule view = EduMajorModule.instance(index, id);
			eduMajorModuleDao.create(view);

			mod = EduModule.instance(arg0.getMajor() + " - 选修课组", "选修课组", 1);
			id = eduModuleDao.create(mod);
			view = EduMajorModule.instance(index, id);
			eduMajorModuleDao.create(view);

		}

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduMajor arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 Update 】- 开始运行 ");

		eduMajorDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 Update 】- 完成运行 ");
	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 Delete 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		eduMajorDao.delete(map);
		eduMajorModuleDao.deleteByMajorId(map);
		eduProjectMajorDao.deleteByMajorId(map);

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final EduMajorReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryCount 】- 开始运行 ");

		Map<String, Object> map = initQuery(arg0);

		Integer index = eduMajorDao.queryCount(map);

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryCount 】- 完成运行 ");
		return index;
	}

	@Override
	public EduMajor queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduMajor entity = eduMajorDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public EduMajor queryByBunchId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryByBunchId 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduMajor entity = eduMajorDao.queryByBunchId(map);

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryByBunchId 】- 完成运行 ");
		return entity;
	}

	@Override
	public ListRep queryList(final EduMajorReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = initQuery(arg0);

		Integer count = eduMajorDao.queryCount(map);

		List<EduMajor> array = eduMajorDao.queryList(map);

		List<EduMajorRep> list = new ArrayList<EduMajorRep>();
		int seq = 1;

		Iterator<EduMajor> it = array.iterator();
		while (it.hasNext()) {
			EduMajor major = it.next();
			Integer modCount = eduMajorModuleServ.queryCountById(major.getId());
			EduMajorRep majorRep = EduMajorRep.instance(major, seq, modCount);
			list.add(majorRep);
			seq++;
		}

		ListRep rep = ListRep.instance(count, list);

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public ListRep queryList(final Integer arg0, final EduMajorReq arg1) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduMajor> array = eduMajorDao.queryList();
		array.removeAll(queryListById(arg0));

		Iterator<EduMajor> it = array.iterator();
		while (it.hasNext()) {
			EduMajor major = it.next();
			if (null != arg1.getMajor() && !arg1.getMajor().equals("") && !major.getMajor().contains(arg1.getMajor()))
				it.remove();
		}

		List<EduMajorRep> list = new ArrayList<EduMajorRep>();
		int seq = arg1.getPage().getStartPage() + 1;

		for (int i = 0; i < array.size(); i++) {
			if (i >= arg1.getPage().getStartPage() && i <= arg1.getPage().getEndPage()) {
				EduMajor major = array.get(i);
				Integer count = eduMajorModuleServ.queryCountById(major.getId());
				EduMajorRep chapRep = EduMajorRep.instance(major, seq, count);
				list.add(chapRep);
				seq++;
			}
		}

		logger.debug(" 未添加的专业 " + list.toString());

		ListRep rep = ListRep.instance(array.size(), list);

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public List<EduMajor> queryListById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduMajor> array = eduMajorDao.queryListById(map);

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduMajorRep> queryListForExam(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduMajor> array = eduMajorDao.queryListById(map);

		List<EduMajorRep> list = new ArrayList<EduMajorRep>();
		int seq = 1;

		Iterator<EduMajor> it = array.iterator();
		while (it.hasNext()) {
			EduMajor major = it.next();
			Integer modCount = eduMajorModuleServ.queryCountById(major.getId());
			EduMajorRep majorRep = EduMajorRep.instance(major, seq, modCount);
			list.add(majorRep);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduMajorServ 】- 【 QueryList 】- 完成运行 ");
		return list;
	}

	@Override
	public List<EduMajorRep> queryRepListById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduMajorRep> array = eduMajorDao.queryRepListById(map);
		int seq = 1;

		Iterator<EduMajorRep> it = array.iterator();
		while (it.hasNext()) {
			EduMajorRep major = it.next();
			Integer majorCount = eduMajorModuleServ.queryCountById(major.getId());
			major = EduMajorRep.instance(major, seq, majorCount);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	private Map<String, Object> initQuery(EduMajorReq arg0) {

		if (null == arg0)
			return null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startPage", arg0.getPage().getStartPage());
		map.put("size", arg0.getPage().getSize());

		StringBuffer query = new StringBuffer("");

		if (null != arg0.getMajor() && arg0.getMajor().length() > 0) {
			query.append("major LIKE :major AND ");
			map.put("major", "%" + arg0.getMajor() + "%");
		}

		String sql = query.toString();
		if (null != sql && sql.length() > 0)
			map.put("query", " WHERE " + sql.substring(0, sql.length() - 4));
		else
			map.put("query", "");

		return map;
	}

}
